package com.demo.rabbitmq.dlx;

import com.demo.rabbitmq.ack.MyConsumer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author sc
 * Created on  2018/9/16
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPort(5672);
        factory.setHost("localhost");
        factory.setVirtualHost("/");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();


        /**
         * 死信队列
         */
        String exchange = "dlx.ex";
        String routingKey = "#";
        String queue_name = "dlx.q";
        channel.exchangeDeclare(exchange,"topic",true);
        channel.queueDeclare(queue_name,false,false,false,null);
        channel.queueBind(queue_name,exchange,routingKey);

        /**
         * 普同对列
         */
        String test_ex="test.dlx.ex";
        String test_q = "test.dlx.q";
        Map<String,Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange",exchange);
        arg.put("x-message-ttl",10000);
        channel.exchangeDeclare(test_ex,"topic",true);
        channel.queueDeclare(test_q,false,false,false,arg);
        channel.queueBind(test_q,test_ex,routingKey);


        channel.basicConsume(queue_name, false, new MyConsumer(channel));


    }
}
